#include "index.h"

char *filename_position(char *buf) {
	int num;
	int i;
	char *tmp;
	
	tmp = buf;
	if(isDosLine(buf))
		num = 3;
	else if(isUnixLine(buf))
		num = 8;
	else
		return NULL;

	for(i = 0; i < num; i++) {
		while(!isspace(*++tmp));
		while(isspace(*++tmp));
	}

	return tmp;
}

void month_aton(char *tmp,char array[2]) {
	if(!strncasecmp(tmp,"Jan",3)) {
		array[0] = '0';
		array[1] = '1';
	}
	else if(!strncasecmp(tmp,"Feb",3)) {
		array[0] = '0';
		array[1] = '2';
	}
	else if(!strncasecmp(tmp,"Mar",3)) {
		array[0] = '0';
		array[1] = '3';
	}
	else if(!strncasecmp(tmp,"Apr",3)) {
		array[0] = '0';
		array[1] = '4';
	}
	else if(!strncasecmp(tmp,"May",3)) {
		array[0] = '0';
		array[1] = '5';
	}
	else if(!strncasecmp(tmp,"Jun",3)) {
		array[0] = '0';
		array[1] = '6';
	}
	else if(!strncasecmp(tmp,"Jul",3)) {
		array[0] = '0';
		array[1] = '7';
	}
	else if(!strncasecmp(tmp,"Aug",3)) {
		array[0] = '0';
		array[1] = '8';
	}
	else if(!strncasecmp(tmp,"Sep",3)) {
		array[0] = '0';
		array[1] = '9';
	}
	else if(!strncasecmp(tmp,"Oct",3)) {
		array[0] = '1';
		array[1] = '0';
	}
	else if(!strncasecmp(tmp,"Nov",3)) {
		array[0] = '1';
		array[1] = '1';
	}
	else if(!strncasecmp(tmp,"Dec",3)) {
		array[0] = '1';
		array[1] = '2';
	}

	return;	
}


int isDosLine(char *buf) {
	return isdigit(buf[0]);
}

int isUnixLine(char *buf) {
	return buf[0] == 'd' || buf[0] == '-';
}

int setDosLine(struct attr *pAttr,int attrId,char *buf) {
	char *tmp;

	pAttr->attrId = attrId;

	pAttr->datetime[0] = buf[0];
	pAttr->datetime[1] = buf[1];
	pAttr->datetime[2] = buf[3];
	pAttr->datetime[3] = buf[4];

	tmp = buf;
	while(!isspace(*++tmp));
	while(isspace(*++tmp));

	pAttr->datetime[4] = tmp[0];
	pAttr->datetime[5] = tmp[1];
	pAttr->datetime[6] = tmp[3];
	pAttr->datetime[7] = tmp[4];
	pAttr->datetime[8] = 0;

	while(!isspace(*++tmp));
	while(isspace(*++tmp));

	if(isdigit(*tmp)) {
		sscanf(tmp,"%llu",&pAttr->fileSize);
	}
	else {
		pAttr->fileSize = 0;
	}

	return 0;
}

int setUnixLine(struct attr *pAttr,int attrId,char *buf) {

	int num;
	int i;
	char *tmp;

	pAttr->attrId = attrId;

	num = 4;
	tmp = buf;
	
	for(i = 0; i < num; i++) {
		while(!isspace(*++tmp));
		while(isspace(*++tmp));
	}

	sscanf(tmp,"%llu",&pAttr->fileSize);

	while(!isspace(*++tmp));
	while(isspace(*++tmp));
	
	month_aton(tmp,pAttr->datetime);

	while(!isspace(*++tmp));
	while(isspace(*++tmp));

	if(isdigit(tmp[1])) {
		pAttr->datetime[2] = tmp[0];
		pAttr->datetime[3] = tmp[1];
	}
	else {
		pAttr->datetime[2] = '0';
		pAttr->datetime[3] = tmp[0];
	}

	while(!isspace(*++tmp));
	while(isspace(*++tmp));

	if(tmp[2] != ':') {
		memset(&pAttr->datetime[4],'0',4);
	}
	else {
		pAttr->datetime[4] = tmp[0];
		pAttr->datetime[5] = tmp[1];
		pAttr->datetime[6] = tmp[3];
		pAttr->datetime[7] = tmp[4];
	}

	return 0;
}

int set_attr(struct attr *pAttr,int attrId,char *buf) {
	//printf("buf=%s",buf);
	if(isDosLine(buf)) {
		return setDosLine(pAttr,attrId,buf);
	}
	else if(isUnixLine(buf)) {
		return setUnixLine(pAttr,attrId,buf);
	}
	
	return 0;
}

bool index2::operator<(const index2 &other) {
	return this->count < other.count;
}
